<?php
session_start();
if ($_SESSION['Status'] != '1') {
    header('Location: signup.php');
}

$id = $_SESSION['name'];

$action = $_GET['action'];

include 'dbconnection.php';

switch ($action) {
    case "INSERT":
        $taskName = $_POST['taskName'];
        $type = $_POST['type'];
        $sDate = $_POST['s_date'];
        $eTime = $_POST['e_time'];
        $sTime = $_POST['s_time'];
        $eDate = $_POST['e_date'];
        $desc = $_POST['desc'];

        $query = "Insert INTO tasks (name,type,Users_email,s_date,e_date,description,e_time,s_time)VALUES ('$taskName','$type','$id', '$sDate', '$eDate', '$desc', '$eTime', '$sTime')";
        if (!$result = mysql_query($query)) {
            die("400");
        } else {
            echo "200";
        }
        break;
    case "UPDATE":
        $taskId = $_POST['task_id'];

        $query = "UPDATE tasks SET " .
                ((isset($_POST['taskName'])) ? "name = '{$_POST['taskName']}' " : " ") .
                ((isset($_POST['s_date'])) ? "s_date = '{$_POST['s_date']}', " : " ") .
                ((isset($_POST['e_date'])) ? "e_date = '{$_POST['e_date']}' " : " ") .
                ((isset($_POST['s_time'])) ? ", s_time = '{$_POST['s_time']}' " : " ") .
                ((isset($_POST['e_time'])) ? ", e_time = '{$_POST['e_time']}' " : " ") .
                ((isset($_POST['desc'])) ? ", description = '{$_POST['desc']}' " : " ") . " WHERE task_id={$taskId}";
        if (!$result = mysql_query($query)) {
            die("400");
        } else {
            echo "200";
        }
        
        break;
    case "DELETE":
        $taskId = $_POST['task_id'];

        $query = " DELETE FROM tasks WHERE task_id={$taskId}";
        if (!$result = mysql_query($query)) {
            die("400");
        } else {
            echo "200";
        }

        break;
    case "GET":
        $query = "Select * From tasks";
        if (!$result = mysql_query($query)) {
            die("400");
        } else {
            $alldata;
            for ($counter = 0; $row = mysql_fetch_assoc($result); $counter++) {
                $alldata[$counter]['title'] = $row['name'];
                $alldata[$counter]['task_id'] = $row['task_id'];
                $alldata[$counter]['start'] = $row['s_date']." ".$row['s_time'];
                $alldata[$counter]['end'] = $row['e_date']." ".$row['e_time'];
                $alldata[$counter]['s_time'] = $row['s_time'];
                $alldata[$counter]['e_time'] = $row['e_time'];
                $alldata[$counter]['description'] = $row['description'];
                $alldata[$counter]['Users_email'] = $row['Users_email'];
                $alldata[$counter]['allDay'] = false;
                $alldata[$counter]['backgroundColor'] = ($row['type']=="Reminder")?"green":(($row['type']=="Event")?"pink":(($row['type']=="Assignment")?"red":"green"));
            }
            header('Content-Type: application/json');
            echo json_encode($alldata);
        }
        break;
}

include 'dbconnectionclose.php';
?>